<?php
class Home_model extends MY_Model {
	private $table = 'photo';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('content').'%" OR `content` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('content').'%" OR `content` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			$data = array(
				'type'			=> $this->input->post('typeAdmincp'),
				'album_id'		=> $this->input->post('albumAdmincp'),
				'title'			=> $this->input->post('titleAdmincp'),
				'description'	=> $this->input->post('descAdmincp'),
				'content'		=> $this->input->post('contentAdmincp'),
				'image'			=> $fileName,
				'status'		=> $status,
				'created'		=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				return true;
			}
		}else{
			//Xử lý xóa hình khi update thay đổi hình
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			if($fileName==''){
				$fileName = $result[0]->image;
			}else{
				@unlink(BASEFOLDER.DIR_PHOTO.$result[0]->image);
			}
			$data = array(
				'title'			=> $this->input->post('titleAdmincp'),
				'description'	=> $this->input->post('descAdmincp'),
				'content'		=> $this->input->post('contentAdmincp'),
				'image'			=> $fileName,
				'status'		=> $status
			);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	//FRONT_END 
	public function getListPost()
	{
		$this->db->select('image,title,description');
		$this->db->where('status',1);
		$this->db->order_by('created','desc');
		$query = $this->db->get(PREFIX.'album');
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	
	public function getSlideShow()
	{
		$this->db->select('image,title,description');
		$this->db->where('status',1);
		$this->db->where('is_home',1);
		$this->db->order_by('created','desc');
		$query = $this->db->get(PREFIX.$this->table);
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	
	public function getListAudio()
	{
		$this->db->select('title,link,id');
		$this->db->where('status',1);
		$this->db->where('type',1);
		$this->db->order_by('album_id','DESC');
		$query = $this->db->get(PREFIX.'album_detail');
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
}